import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.util.ArrayList;

public class Demo7 {

     public static class map extends Mapper<LongWritable,Text,Text,Text>{
         @Override
         protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
             InputSplit is = context.getInputSplit();
             FileSplit fs=(FileSplit) is;
             String s = fs.getPath().toString();

             if (s.contains("students")){
                 String s1 = "*"+value.toString();
                 String s2 = value.toString().split(",")[0];
                 context.write(new Text(s2),new Text(s1));
             }else {
                 String s1 = "#"+value.toString();
                 String s2 = value.toString().split(",")[0];
                 context.write(new Text(s2),new Text(s1));
             }
         }
     }

     public static class reduce extends Reducer<Text,Text,Text,NullWritable>{
         @Override
         protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {

             String st="";
             ArrayList<String> sc = new ArrayList<String>();

             for (Text value : values) {
                 String s = value.toString();
                 if (s.startsWith("*")){
                     st = s.substring(1);
                 }else {
                     sc.add(s.substring(1));
                 }
             }

             int sum=0;
             for (String s : sc) {
                 Integer i = Integer.valueOf(s.split(",")[2]);
                 sum+=i;
             }

             String end=st+","+sum;
             context.write(new Text(end),NullWritable.get());
         }
     }

    public static void main(String[] args) throws Exception{
        Job job = Job.getInstance();
        job.setJobName("求学生成绩总和");
        job.setJarByClass(Demo7.class);

        job.setMapperClass(map.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        job.setReducerClass(reduce.class);
        job.setOutputKeyClass(Text.class);
        job.setMapOutputValueClass(NullWritable.class);

        Path in = new Path("/datajava");
        FileInputFormat.addInputPath(job,in);

        Path out = new Path("/output7");
        FileSystem fs = FileSystem.get(new Configuration());
        if (fs.exists(out)){
            fs.delete(out,true);
        }
        FileOutputFormat.setOutputPath(job,out);

        job.waitForCompletion(true);
        System.out.println("你又成功了");


    }
}
